﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using WinAutomator.Library;
using WinAutomator.Library.InOut;
using WinAutomator.Plugin;

namespace WinAutomator.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class DiagramEditorViewModel : ViewModelBase
    {
//        IPlugin _root;
        ObservableCollection<IPlugin> _items;
        ObservableCollection<Connection> _itemsConnections;
        private IPlugin _root = null;

        private WAConnectorViewModel _virtualConnectorSelected;

        /// <summary>
        /// Initializes a new instance of the DiagramEditorViewModel class.
        /// </summary>
        /// 
        public DiagramEditorViewModel()
        {
            _items = new ObservableCollection<IPlugin>();
            _itemsConnections = new ObservableCollection<Connection>();

            Messenger.Default.Register<IPlugin>(this, "SelectPluginToolBox", plugin =>
            {
                IPlugin obj = PluginManager.Instance.CreateInstance(plugin);
                if (RootPlugin == null)
                    RootPlugin = obj;

                Items.Add(obj);
            });

            Messenger.Default.Register<WAConnectorViewModel>(this, "ClickConnector", vConnector =>
            {
                if (_virtualConnectorSelected == null)
                    _virtualConnectorSelected = vConnector;
                else
                {
                    bool result = _virtualConnectorSelected.Model.TryToConnect(vConnector.Model);
                    if (result == true)
                    {
                        MessageBox.Show("ok");
                        Connection tmp = new Connection(_virtualConnectorSelected, vConnector);
                        _itemsConnections.Add(tmp);
                    }
                    else
                    {
                        MessageBox.Show("ko");
                    }
                    _virtualConnectorSelected = null;
                }
            });

            Messenger.Default.Register<object>(this, "RunWorkflow",  plugin =>
            {
                WAWorkflow wf = new WAWorkflow();
                wf.SetRoot(RootPlugin.ViewModel.Model);
                wf.Start();
            });

            Messenger.Default.Register<IPlugin>(this, "SetRootPlugin", plugin =>
            {
                RootPlugin = plugin;
            });
        }

        public ObservableCollection<IPlugin> Items
        {
            get { return _items; }
        }

        public ObservableCollection<Connection> ItemsConnections
        {
            get { return _itemsConnections; }
        }

        /// <summary>
        /// The <see cref="RootPlugin" /> property's name.
        /// </summary>
        public const string RootPluginPropertyName = "RootPlugin";

        

        /// <summary>
        /// Sets and gets the RootPlugin property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public IPlugin RootPlugin
        {
            get
            {
                return _root;
            }

            set
            {
                if (_root == value)
                {
                    return;
                }

                _root = value;
                RaisePropertyChanged(RootPluginPropertyName);
            }
        }
    }
}